package com.itheima.CSM_bacK.aaaControllers;

import com.itheima.CSM_bacK.aabServices.Serv.ComServ;
import com.itheima.common.entity.Result;
import com.itheima.pojo.VO.VOrGetBRD;
import com.itheima.pojo.VO.VOrGetMMs;
import com.itheima.pojo.VO.VOrGetSR;
import com.itheima.pojo.VO.voTip2;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.util.List;

@RestController
@RequestMapping("/report")
public class ComController {
    @Autowired
    private ComServ comServ;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport() {
        VOrGetMMs kks = comServ.getMemberReport();
        return new Result(true, "获取会员统计数据成功", kks);
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport() {
        VOrGetSR kks = comServ.getSetmealReport();
        return new Result(true, "获取会员统计数据成功", kks);
    }

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        VOrGetBRD kks = comServ.getBusinessReportData();
        return new Result(true, "获取会员统计数据成功", kks);
    }

    @Autowired
    ResourceLoader resourceLoader;

    @RequestMapping("/exportBusinessReport")
    public String exportBusinessReport(HttpServletResponse response) throws Exception {
        // 读取模板文件
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("templates/report_template.xlsx");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("report_template.xlsx", "UTF-8"));

        XSSFWorkbook workbook = null;
        if (is != null) {
            workbook = new XSSFWorkbook(is);
        }
        // 获取模板中的第一个工作表
        XSSFSheet sheet = null;
        if (workbook != null) {
            sheet = workbook.getSheetAt(0);
        }
        else System.out.println("11111");
        VOrGetBRD kks = comServ.getBusinessReportData();
        // 在模板工作表中操作数据
        sheet.getRow(2).getCell(5).setCellValue(String.valueOf(kks.getReportDate()));

        sheet.getRow(4).getCell(5).setCellValue(kks.getTodayNewMember());
        sheet.getRow(4).getCell(7).setCellValue(kks.getTotalMember());

        sheet.getRow(5).getCell(5).setCellValue(kks.getThisWeekNewMember());
        sheet.getRow(5).getCell(7).setCellValue(kks.getThisMonthNewMember());

        sheet.getRow(7).getCell(5).setCellValue(kks.getTodayOrderNumber());
        sheet.getRow(8).getCell(5).setCellValue(kks.getThisWeekOrderNumber());
        sheet.getRow(9).getCell(5).setCellValue(kks.getThisMonthOrderNumber());

        sheet.getRow(7).getCell(7).setCellValue(kks.getTodayVisitsNumber());
        sheet.getRow(8).getCell(7).setCellValue(kks.getThisWeekVisitsNumber());
        sheet.getRow(9).getCell(7).setCellValue(kks.getThisMonthVisitsNumber());

        sheet.getRow(12).getCell(4).setCellValue(String.valueOf(kks.getReportDate()));
        sheet.getRow(13).getCell(4).setCellValue(String.valueOf(kks.getReportDate()));
        sheet.getRow(14).getCell(4).setCellValue(String.valueOf(kks.getReportDate()));
        sheet.getRow(15).getCell(4).setCellValue(String.valueOf(kks.getReportDate()));

        List<voTip2> hhh = kks.getHotSetmeal();
        for (int i = 0; i < hhh.size(); i++) {
//            Row row = sheet.getRow(i + 12);
            sheet.getRow(i + 12).getCell(4).setCellValue(hhh.get(i).getName());
            sheet.getRow(i + 12).getCell(5).setCellValue(hhh.get(i).getSetmeal_count());
            sheet.getRow(i + 12).getCell(6).setCellValue(hhh.get(i).getProportion());
            sheet.getRow(i + 12).getCell(7).setCellValue("暂无");
        }
        // 将修改后的内容写入响应流
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("report_template.xlsx", "UTF-8"));
        OutputStream os = response.getOutputStream();

        workbook.write(os);


        return "下载文件";
    }


}
